Lab2


4530402121_4530403821  นาย ภาติไนย เอื้อวิจิตรกุล และ นาย ภาณุวัฒน์ ชัยสนิท (4/7/2545 (8:48:57))
(SM=2, CM=16, ST=6, KY=0, TR=00:10)

MiniQuiz + TestScript
Mini-Quiz : Q1=3,Q2=2,Q3=3,Q4=2,Q5=1  (3.0 คะแนน)

JLab>java TestScript
>>JLab:Testing -> horizontal : ok 
>>JLab:Testing -> vertical : ok 
>>JLab:Testing -> 0-distance : ok 
>>JLab:Testing -> random : ok ok 
>>JLab:OK      -> 1, 1, 1, 2
>>JLab:<POINT>4.0</POINT>
JLab>

ได้ 7 คะแนน
Applet
Source Code
/**
 * 2110101 Computer Programming
 * Lab #2 : Arithmetic Expression
 * @author somchaip@chula.ac.th
 * @version 1.00 27/06/2002
 */
import jlab.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Lab2 extends Applet {
  
  /**
   * This method computes (px, py) which is the intersection point
   * obtained by drawing a line from (cx, cy), the center of a circle
   * whose radius is cr, to (mx, my), a mouse pointer position.
   * @param cx, cy - circle center's coordinate
   * @param cr - circle's radius
   * @param mx, my - mouse pointer's coordinate
   * @return <code>Point</code> of the intersection
   */
  public static Point compute(int cx, int cy, int cr, int mx, int my) {
    int px, py;

    //---- begin your code here ----
    double d;
    d = Math.sqrt ((mx - cx) * (mx - cx) + (my - cy) * (my - cy));
     py = (int) ((my - cy) * cr / d) + cy;
     px = (int)((mx - cx) * cr / d) + cx;
     





    //---- end your code here ------
       
    return new Point(px, py);
  } // end of compute

  //--------------------------------------------------------------------
  private Point mouse;
  private Point leftEye, rightEye, leftPupil, rightPupil;
  private int curWidth = 0, curHeight = 0;
  private Image imageBuffer;
  private Graphics gBuffer;
  private final int EYE_RADIUS = 20, PUPIL_RADIUS = 5;

  /**Initialize the applet*/
  public void init() {
    this.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
        public void mouseMoved(MouseEvent e) {
          this_mouseMoved(e);
        }
      }
    );
  }

  private void this_mouseMoved(MouseEvent e) {
    mouse = e.getPoint();
    repaint();
  }

  /**Start the applet*/
  public void start() {
    mouse = new Point(getSize().width / 2, 10000);
    setEyePos(new Point (getSize().width / 2, getSize().height / 2));
    repaint();
  }

  public void paint(Graphics g) {
    update(g);
  }

  public void update(Graphics g) {
    int newWidth = getSize().width;
    int newHeight = getSize().height;

    // implement double buffering to reduce screen flicker
    if (imageBuffer == null ||
        newWidth != curWidth || newHeight != curHeight) {
      imageBuffer = createImage(newWidth, newHeight);
      System.out.println("w = " + newWidth + ", h = " + newHeight);
      if (gBuffer != null) gBuffer.dispose();
      gBuffer = imageBuffer.getGraphics();
      curWidth = newWidth;
      curHeight = newHeight;
      setEyePos(new Point(curWidth / 2, curHeight / 2));
    }
    gBuffer.setColor(Color.pink);
    gBuffer.fillRect(0, 0, newWidth, newHeight);
    drawEye(gBuffer);
    g.drawImage(imageBuffer, 0, 0, this);
  }

  private void setEyePos(Point center) {
    leftEye = new Point(center.x - EYE_RADIUS - 3, center.y);
    rightEye = new Point(center.x + EYE_RADIUS + 3, center.y);
  }

  private void drawEye(Graphics g) {
    // Draw the white eyes
    g.setColor(Color.white);
    fillCircle(g, leftEye, EYE_RADIUS);
    fillCircle(g, rightEye, EYE_RADIUS);

    g.setColor(Color.black);
    g.drawOval(leftEye.x - EYE_RADIUS, leftEye.y - EYE_RADIUS,
      2 * EYE_RADIUS, 2 * EYE_RADIUS);
    g.drawOval(rightEye.x - EYE_RADIUS, rightEye.y - EYE_RADIUS,
      2 * EYE_RADIUS, 2 * EYE_RADIUS);

    // Draw the pupils
    g.setColor(Color.black);
    leftPupil = computePupilPos(leftEye);
    fillCircle(g, leftPupil, PUPIL_RADIUS);
    rightPupil = computePupilPos(rightEye);
    fillCircle(g, rightPupil, PUPIL_RADIUS);
  }

  private Point computePupilPos(Point eye) {
    return compute(eye.x, eye.y, EYE_RADIUS - PUPIL_RADIUS,
                   mouse.x, mouse.y);
  }

  private void fillCircle(Graphics g, Point center, int radius) {
    g.fillOval(center.x - radius, center.y - radius,
               2 * radius, 2 * radius);
  }

  //--------------- Main method ---------------------
  public static void main(String[] args) {
    Lab2 applet = new Lab2();

    String title = "JLab : JEyes";
    // set up window frame
    Frame frame = new Frame(title);

    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
          System.exit(0);
        }
      }
    );
    frame.add(applet, BorderLayout.CENTER);
    frame.setSize(300, 300);
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();

    frame.setLocation((d.width - frame.getSize().width) / 2,
      (d.height - frame.getSize().height) / 2);
    applet.init();
    applet.start();
    frame.setVisible(true);
    try {
      JLabIO.setWindowAlwaysOnTop(title);
    } catch (Error e) {}
  }

}
//-------------------------------------------------------------

Answer Sheet

แผ่นคำตอบสำหรับปฏิบัติการ


จงตอบคำถามข้างล่างนี้เมื่อเขียนโปรแกรมตามข้อกำหนดเสร็จแล้ว

ไปที่คลาส Lab2 ที่ได้เพิ่มส่วนของโปรแกรมตามข้อกำหนดเสร็จแล้ว คราวนี้กดปุ่ม F5 เพื่อสั่งคลาสให้ทำงาน (ไม่ใช่สั่ง Run Test Script ซึ่งใช้กดปุ่ม F6) ดูผลที่ได้  แล้วอธิบายด้วยว่าส่วนของโปรแกรมที่เขียนขึ้นนั้น ไปเกี่ยวอะไรด้วยกับผลที่เห็นบนจอภาพ